package com.weiiew.buildtypeservice.controller;

import com.weiiew.buildtypeservice.service.BuildingTypeServiceImpl;
import com.weiiew.common.entity.dto.BuildingType;
import com.weiiew.common.GatawayAccess.GatewayAccess;
import com.weiiew.common.utils.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author 魏锴
 * @Date 2024/8/6
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/type/build")
@GatewayAccess(GatewayAccess.GatewayType.MANAGER)
public class BuildTypeController {
    private final BuildingTypeServiceImpl service;

    @GetMapping("/list")
    public ApiResponse getItemTypeList(@RequestHeader("my-version") Integer version, String name) {
        System.out.println("name = " + name);
        return ApiResponse.ok("获取成功")
                .append("list", service.getBuildTypes(name, version));
    }

    @PostMapping("/rename")
    public ApiResponse renameType(@RequestHeader("my-version") Integer version, Integer id, String name) {
        if (id == null || name == null) return ApiResponse.failure("参数错误");
        if (service.renameType(id, name, version)) return ApiResponse.ok("修改成功");
        return ApiResponse.failure("修改失败");
    }

    @PostMapping("/create")
    public ApiResponse createType(@RequestHeader("my-version") Integer version, String name) {
        if (name == null || version == null) return ApiResponse.failure("输入不能为空");
        BuildingType type = new BuildingType();
        type.setName(name);
        type.setVersion(version);
        if (service.createType(type)) return ApiResponse.ok("保存成功");
        else return ApiResponse.failure("保存失败");
    }

    @PostMapping("/remove")
    public ApiResponse removeType(@RequestHeader("my-version") Integer version, Integer id) {
        if (id == null) return ApiResponse.failure("id为空");
        return ApiResponse.response(service.removeType(id, version), "建筑种类删除");
    }
}
